package com.yunche.yunchebusiness.ui.fragment

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import com.afollestad.materialdialogs.MaterialDialog
import com.yunche.yunchebusiness.R
import com.yunche.yunchebusiness.api.Api
import com.yunche.yunchebusiness.base.BaseFragment
import com.yunche.yunchebusiness.common.CommonDialog
import com.yunche.yunchebusiness.common.Constant
import com.yunche.yunchebusiness.mvp.contract.UpdateContract
import com.yunche.yunchebusiness.mvp.presenter.UpdatePresenter
import com.yunche.yunchebusiness.ui.activity.main.ChangePwdActivity
import com.yunche.yunchebusiness.ui.activity.main.LoginActivity
import com.yunche.yunchebusiness.ui.activity.system.*
import com.yunche.yunchebusiness.ui.view.optionView.OptionItemClickListener
import com.yunche.yunchebusiness.utils.CommonUtils
import com.yunche.yunchebusiness.utils.SpHelper
import com.yunche.yunchebusiness.utils.ToastyUtils
import kotlinx.android.synthetic.main.fragment_system.*

/**
 * Create by Crcker on 2018/12/21 13:47
 * Email: Crcker@163.com
 */

@SuppressLint("StaticFieldLeak")

class SystemFragment : BaseFragment(), OptionItemClickListener, UpdateContract.View {


    override fun setLayout(): Int = R.layout.fragment_system

    private val mPresenter by lazy {
        UpdatePresenter()
    }


    companion object {
        private var fragment: SystemFragment? = null
        fun getInstance(): SystemFragment {
            if (fragment == null) {
                fragment = SystemFragment()
            }
            return fragment!!
        }
    }


    override fun hasNewVersion(info: String, downloadUrl: String, isMust: Boolean) {
        MaterialDialog.Builder(activity!!)
            .title("发现新版本")
            .content(info)
            .positiveText("去更新")
            .onPositive { dialog, which ->
                val uri = Uri.parse(downloadUrl)
                val intent = Intent(Intent.ACTION_VIEW, uri)
                startActivity(intent)
            }
            .cancelable(isMust)
            .show()
    }

    override fun noNewVersion() {
        ToastyUtils.showInfo("已是新版本~")
    }

    override fun showLoading() {

        CommonDialog.showLoading(activity!!, "检测版本中")
    }

    override fun hideLoading() {
        CommonDialog.cancle()
    }

    override fun showError(msg: String) {
        ToastyUtils.showError(msg)
    }

    override fun onOptionItemClickListener(position: Int) {
        when (position) {
            0 -> {
                openActivity(TenantInfoActivity::class.java)
            }
            1 -> {
                openActivity(UserInfoActivity::class.java)
            }
            2 -> {
                if (Api.isSetGesture()) {
                    openActivityForResult(ChangeGestureActivity::class.java, null, 1001)
                } else {
                    openActivity(GestureActivity::class.java)
                }
            }
            3 -> {
                mPresenter.checkVersion(CommonUtils.getVersionName(activity!!), 4)
            }
            4 -> {
                CommonUtils.clearAllCache(activity!!)
                ToastyUtils.showSuccess("清除完成")
                ov_system_cache.setContent(CommonUtils.getTotalCacheSize(activity!!))
            }
            6 -> {
                openActivity(ChangePwdActivity::class.java)
            }
            5 -> {
                openActivity(AboutActivity::class.java)
            }
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Constant.RESULT_GESTURE_SUCCESS) {
            openActivity(GestureActivity::class.java)
        }
    }

    override fun initView() {
        mPresenter.attachView(this)
        ov_gesture.setOnOptionItemClickListener(this)
        ov_userinfo_registe.setOnOptionItemClickListener(this)
        ov_tenant_registe.setOnOptionItemClickListener(this)
        ov_system_version.setOnOptionItemClickListener(this)
        ov_system_cache.setOnOptionItemClickListener(this)
        ov_system_change_pwd.setOnOptionItemClickListener(this)
        ov_system_about.setOnOptionItemClickListener(this)

        btn_login_out.setOnClickListener {
            var phone = Api.getUserName()
            SpHelper.clearPreference()
            SpHelper.putValue(Constant.SP_KEY.USER_NAME, phone)
            openActivity(LoginActivity::class.java)
        }
    }

    override fun initData() {
        ov_system_cache.setContent(CommonUtils.getTotalCacheSize(activity!!))
        ov_system_version.setContent("V " + CommonUtils.getVersionName(activity!!))

    }
}